package ar.edu.itba.bundundu;

import java.io.IOException;
import java.net.URL;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ar.edu.itba.bundundu.dao.UserDao;
import ar.edu.itba.bundundu.domain.UserIds;

import com.google.code.facebookapi.FacebookParam;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.Link;
import com.google.gdata.data.acl.AclEntry;
import com.google.gdata.data.acl.AclFeed;
import com.google.gdata.data.acl.AclNamespace;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarFeed;

public class DeleteAccessServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    RequestDispatcher dispatcher;
    dispatcher = getServletContext().getRequestDispatcher("/sharecalendars");
    try {
      Facebook facebook = new Facebook(req
          .getParameter(FacebookParam.SESSION_KEY.toString()));
      Long uid = facebook.getUserId();
      String uids = facebook.getUidsAppFriends();
      req.setAttribute("uid", uid);
      req.setAttribute("uids", uids);
      UserIds user = UserDao.getUser(uid);
      String sessionToken = user.getToken();
      CalendarService service = new CalendarService("exampleCo-exampleApp-1");
      service.setAuthSubToken(sessionToken, null);
      // get the params from the form
      String aclEntry_id = (String) req.getParameter("acl_id");
      String url = (String) req.getParameter("url");
      
      AclEntry delete_entry = null;
      CalendarFeed calendarFeed = service.getFeed(new URL(
          "http://www.google.com/calendar/feeds/default/owncalendars/full"),
          CalendarFeed.class);
      for (CalendarEntry calEntry : calendarFeed.getEntries()) {
        Link link = calEntry.getLink(AclNamespace.LINK_REL_ACCESS_CONTROL_LIST,
            Link.Type.ATOM);
        if (calEntry.getId().toString().equals(url)) {
          AclFeed aclFeed = service.getFeed(new URL(link.getHref()),
              AclFeed.class);
          for (AclEntry aclEntry : aclFeed.getEntries()) {
            if (aclEntry.getId().toString().equals(aclEntry_id)) {
              delete_entry = aclEntry;
            }
          }
        }
      }
      if (delete_entry != null) {
        try {
          delete_entry.delete();
        } catch (Exception e) {
          req.setAttribute("error", "error en el delete: " + e.getMessage());
        }
      } else {
        req.setAttribute("error",
                "We are sorry, but we can't delete that entry"
                    + " right now.  Please try again later, and if the error persists, go "
                    + "to google calendar and delete the entry from there. ");
      }
      dispatcher.forward(req, resp);
    } catch (Exception e) {
      req.setAttribute("error", e.getMessage());
      dispatcher.forward(req, resp);
      //e.printStackTrace();
    }
  }
}
